<?php
class Dig_Ui_Skin
{
    public $scope;
    public $template;
    public $skin;
    public $layout;
    protected $_skinData;
    protected $_templatePath;
    protected $_view;
    public function __construct ($skin, $template = 'default', $scope = 'public')
    {
        $this->skin = $skin;
        $this->template = $template;
        $this->scope = $scope;
        $this->_templatePath = '/templates/' . $this->scope . '/' . $this->template;
        // load the skin config file
        $this->_skinData = new Zend_Config_Xml(APPLICATION_PATH . '/../public_html' . $this->_templatePath . '/skins/' . $this->skin . '.xml');
        $this->layout = new stdClass();
        $this->layout->script = $this->_skinData->layout;
        $this->layout->path = APPLICATION_PATH . '/../public_html' . $this->_templatePath . '/layouts';
        return $this;
    }
    public function getStyleSheets ()
    {
        $files = array();
        if (isset($this->_skinData->styles->stylesheet)) {
            if (is_object($this->_skinData->styles->stylesheet)) {
                $stylesheets = $this->_skinData->styles->stylesheet->toArray();
            } else {
                $stylesheets = array($this->_skinData->styles->stylesheet);
            }
            foreach ($stylesheets as $stylesheet) {
                if (substr($stylesheet, 0, 1) == '/') {
                    // create a public root relative link
                    $filePath = $stylesheet;
                } else {
                    // create a link relative to the template's css folder
                    $filePath = $this->_templatePath . '/css/' . $stylesheet;
                }
                $files[] = $filePath;
            }
        }
        return $files;
    }
    public function getJavascriptFiles ($rel = 'head')
    {
        $files = array();
        if (isset($this->_skinData->javascript->$rel->script)) {
            $scriptData = $this->_skinData->javascript->$rel->script;
            if (is_object($scriptData)) {
                $scripts = $scriptData->toArray();
            } else {
                $scripts = array($scriptData);
            }
            if (is_array($scripts)) {
                foreach ($scripts as $src) {
                    if (substr($src, 0, 1) == '/') {
                        // create a public root relative link
                        $filePath = $src;
                    } else {
                        // create a link relative to the template's css folder
                        $filePath = $this->_templatePath . '/javascript/' . $src;
                    }
                    $files[] = $filePath;
                }
            }
        }
        return $files;
    }
    public function getlayout ()
    {
        return $this->layout;
    }
    public static function load ($skin, $template = 'default', $scope = 'public')
    {
        $skin = new self($skin, $template, $scope);
        $view = $skin->getView();
        $styleSheets = $skin->getStyleSheets();
        if (count($styleSheets) > 0) {
            foreach ($styleSheets as $styleSheet) {
                $view->headLink()->appendStylesheet($styleSheet);
            }
        }
        $javascript = $skin->getJavascriptFiles();
        if (is_array($javascript) && count($javascript) > 0) {
            foreach ($javascript as $script) {
                $view->headScript()->appendFile($script);
            }
        }
        $jquery = $skin->getJavascriptFiles('jquery');
        if (is_array($javascript) && count($javascript) > 0) {
            foreach ($javascript as $script) {
                $view->jQuery()->addJavascriptFile($script);
            }
        }
        return $skin;
    }
    /**
     * Get current view object
     *
     * If no view object currently set, retrieves it from the ViewRenderer.
     * 
     * @todo Set inflector from view renderer at same time
     * @return Zend_View_Interface
     */
    public function getView ()
    {
        if (null === $this->_view) {
            require_once 'Zend/Controller/Action/HelperBroker.php';
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
            if (null === $viewRenderer->view) {
                $viewRenderer->initView();
            }
            $this->setView($viewRenderer->view);
        }
        return $this->_view;
    }
    /**
     * Set view object
     * 
     * @param  Zend_View_Interface $view
     * @return Zend_Layout
     */
    public function setView (Zend_View_Interface $view)
    {
        $this->_view = $view;
        return $this;
    }
}
?>